package mya.mes.mdc.logic.init;


import mya.mes.mdc.logic.dispatch.core.AlarmDispatchManager;
import mya.mes.mdc.logic.dispatch.core.SysManager;
import mya.mes.mdc.logic.dispatch.executor.DispatchExecutor;
import mya.mes.mdc.logic.mqtt.MdcMqttDefaultProcessor;
import mya.mes.mdc.logic.tag.PlcTagCache;
import mya.mes.mdc.model.system.RegularReminders;
import mya.mes.mdc.mqtt.MqttClientManager;
import mya.mes.mdc.share.api.MdcInitService;
import mya.mes.mdc.share.vo.MdcOptions;

import java.util.List;

public class MdcInitServiceImpl implements MdcInitService {


    @Override
    public void init(MdcOptions mdcOptions) throws Exception {
        if (mdcOptions.MQTT_RECEIVE_DEFAULT_PROCESSOR) {
            mdcOptions.MQTT_RECEIVE_PROCESSOR = new MdcMqttDefaultProcessor();
        } else if (mdcOptions.MQTT_RECEIVE_PROCESSOR == null) {
            throw new Exception("没有定义处理接收MQTT消息的接口的实现");
        }
        mdcOptions.ALARM_SENT_TO_WEIXIN = true;
        PlcTagCache.getInstance().updateCache();
        PlcTagCache.getInstance().initTagType();
        MqttClientManager.getInstance().start();
        PlcTagCache.getInstance().initRegularReminders();
        List<RegularReminders> regularReminderses = PlcTagCache.getInstance().getRegularReminderses();
        SysManager.getInstance().start(regularReminderses);
        if (mdcOptions.ALARM_SENT_TO_WEIXIN) {
            AlarmDispatchManager.getInstance().start();
            DispatchExecutor.getInstance().start();
        }
    }

}
